home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / recode.lha / recode-3.2.4 / mergelex.awk < prev    next >
Text File  |  1992-08-19  |  3KB  |  149 lines

  1. # Conversion of files between different charsets and usages.
  2. # Copyright (C) 1990 Free Software Foundation, Inc.
  3. # Francois Pinard <pinard@iro.umontreal.ca>, 1990.
  4. #
  5. # This program is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 2, or (at your option)
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but
  11. # WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. # General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.  
  20. # This awk script merges several lex sources intended for recode.
  21.  
  22. BEGIN {
  23.   TEMP_FILE = "mergelex.tmp"
  24.  
  25.   print "%{"
  26.   print "#include \"common.h\""
  27.   print "#ifdef USE_FPUTC"
  28.   print "#define output(ch) fputc (ch, yyout)"
  29.   print "#else"
  30.   print "#define output(ch) putc (ch, yyout)"
  31.   print "#endif"
  32.   print "extern YY_CHAR *yytext;"
  33.   print "extern int yyleng;"
  34.   print "extern FILE *yyin, *yyout;"
  35.   print "%}"
  36. }
  37.  
  38. # A %{ comment in column 1 signals the start of a new file and of a
  39. # C code section ended by a %} line.  This could be followed by a
  40. # few lex definitions.
  41. #
  42. # A %% in column 1 signals the beginning of lex rules.
  43.  
  44. /^%\{/ {
  45.   section = 1
  46.   c_code = 1
  47.   print
  48.   next
  49. }
  50.  
  51. /^%\}/ {
  52.   print
  53.   c_code = 0
  54.   next
  55. }
  56.  
  57. /^%%/ {
  58.   print "%x X_" step_name
  59.   section = 2
  60.   next
  61. }
  62.  
  63. # Remove block C comments.  It is assumed that, when a /* comment
  64. # starts in column 1, there is no code following the closing */ on
  65. # its line.  Also, remove all white lines.
  66.  
  67. /^\/\*/ {
  68.   while (!match ($0, /\*\//))
  69.     getline
  70.   next
  71. }
  72.  
  73. /^[     ]*$/ {
  74.   next
  75. }
  76.  
  77. # In section 1, %{ delimited C code is copied almost verbatim.  For
  78. # the `#define STEP' line, the step name is noted and added to the
  79. # list of known step names, and a start declaration will be generated
  80. # later, when the %% will be met. 
  81.  
  82. $1 == "#define" && $2 == "STEP" {
  83.   step_name = $3
  84.   step_names[$3]++
  85.   next
  86. }
  87.  
  88. c_code {
  89.   print
  90.   next
  91. }
  92.  
  93. # Declarations are studied, declarations having the same name are kept
  94. # only if their definition differ, so to generate a lex error later. 
  95.  
  96. /^[^     ]/ && section == 1 {
  97.   if ($1 in rules) {
  98.     if ($0 == rules[$1]) {
  99.       next
  100.     }
  101.   }
  102.   else {
  103.     rules[$1] = $0
  104.   }
  105.   print
  106.   next
  107. }
  108.  
  109. # In section 2, every line is copied to a temporary file.  While
  110. # doing so, each lex rule is prefixed by a start name derived from
  111. # the step name.
  112.  
  113. /^[     ]/ && section == 2 {
  114.   print > TEMP_FILE
  115.   next
  116. }
  117.  
  118. section == 2 {
  119.   print "<X_" step_name ">" $0 > TEMP_FILE
  120.   next
  121. }
  122.  
  123. # At end, a single %% line is output followed by the contents of the
  124. # temporary file.  Then, a second %% is output and followed by
  125. # generated interfaces, one routine for each step name.
  126.  
  127. END {
  128.   print "%%"
  129.  
  130.   close (TEMP_FILE)
  131.   while (getline <TEMP_FILE)
  132.     print
  133.   close (TEMP_FILE)
  134.  
  135.   print "%%"
  136.   for (step_name in step_names) {
  137.     print "void"
  138.     print step_name " (FILE *input_file, FILE *output_file)"
  139.     print "{"
  140.     print "  yy_init = 1;"
  141.     print "  yyin = input_file;"
  142.     print "  yyout = output_file;"
  143.     print "  BEGIN X_" step_name ";"
  144.     print "  yylex ();"
  145.     print "}"
  146.   }
  147. }
  148.  
  149.